package top.zhdev.community.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.zhdev.community.entity.Event;
import top.zhdev.community.entity.User;
import top.zhdev.community.event.EventProducer;
import top.zhdev.community.service.LikeService;
import top.zhdev.community.service.UserService;
import top.zhdev.community.utils.CommunityConstant;
import top.zhdev.community.utils.CommunityUtil;
import top.zhdev.community.utils.HostHolder;
import top.zhdev.community.utils.RedisKeyUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 点赞
 */
@Controller
public class LikeController implements CommunityConstant {

    private HostHolder hostHolder;
    private LikeService likeService;
    private UserService userService;
    private EventProducer eventProducer;

    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    public void setHostHolder(HostHolder hostHolder){
        this.hostHolder = hostHolder;
    }

    @Autowired
    public void setLikeService(LikeService likeService){
        this.likeService = likeService;
    }

    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    @Autowired
    public void setEventProducer(EventProducer eventProducer){
     this.eventProducer = eventProducer;
    }

   @PostMapping("/like")
   @ResponseBody
    public String like(@RequestParam("entityType") int entityType,
                       @RequestParam("entityId") int entityId,
                       @RequestParam("entityUserId")int entityUserId,
                       @RequestParam("postId") int postId){
        // 获取当前用户
       User user = hostHolder.getUser();
       // 点赞
       likeService.like(user.getId(),entityType,entityId,entityUserId);
       // 点赞数量
       long likeCount = likeService.getEntityLikeCount(entityType,entityId);
       // 点赞状态
       int likeStatus = likeService.getEntityLikeStatus(entityType,entityId,user.getId());
       // 返回结果
       Map<String,Object> map = new HashMap<>();
       map.put("likeCount",likeCount);
       map.put("likeStatus",likeStatus);

       // 触发点赞通知，仅点赞需要通知(未作)
       if(likeStatus == 1){
           Event event = new Event()
                   .setTopic(TOPIC_LIKE)
                   .setUserId(hostHolder.getUser().getId())
                   .setEntityType(entityType)
                   .setEntityId(entityId)
                   .setEntityUserId(entityUserId)
                   .setData("postId",postId);
           eventProducer.sendEvent(event);
       }
       if(entityType == ENTITY_TYPE_POST){
           // 计算帖子分数
           String redisKey = RedisKeyUtil.getPostScoreKey();
           redisTemplate.opsForSet().add(redisKey,postId);
       }

       // 返回json字符串
       return CommunityUtil.getJSONString(0,null,map);
    }

    /**
     * 退出登录
     * @param ticket
     * @return
     */
    @GetMapping("/logout")
    public String logout(@CookieValue("ticket") String ticket){
        userService.logout(ticket);
        SecurityContextHolder.clearContext();
        hostHolder.clear();
        return "redirect:/login";
    }
}
